package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.UserDto;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.admin.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(value = "后台用户登录",tags = "user",description = "后台用户登录api")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public UserVo queryById(@PathVariable("id") String id){
        return userService.queryById(id);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") String id){
        boolean result = userService.deleteId(id);
        return new Result(result,result?"删除用户成功":"删除用户失败",null);
    }
    @ApiOperation("分页查询用户列表数据")
    @PostMapping("/list")
    public PageVo<UserPageVo> queryByPage(@RequestBody PageDto pageDto){
        if(pageDto.getCurrentPage()==null){
            pageDto.setCurrentPage(1);
        }
        if(pageDto.getPageSize()==null){
            pageDto.setPageSize(10);
        }
        return userService.queryByPage( pageDto);
    }

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public Result inserUser(@RequestBody UserDto userDto){
        boolean result = userService.insertUser(userDto);
        return new Result(result,result?"添加用户成功":"添加用户失败",null);
    }
    @ApiOperation("修改用户")
    @PutMapping("/edit")
    public Result updateUser(@RequestBody UserDto userDto){
        System.out.println(userDto);
        boolean result = userService.updateUser(userDto);
        return new Result(result,result?"修改用户成功":"修改用户失败",null);
    }

    @ApiOperation("用户授权")
    @PostMapping("/role")
    public Result userAuth(@RequestBody Map map){
        System.out.println(map);
        boolean result = userService.roleAuth(map);
        return new Result(result,result?"用户授权成功":"用户授权失败",null);
    }

}
